// Decompiled by DJ v3.11.11.95 Copyright 2009 Atanas Neshkov  Date: 1/30/2010 10:57:20 PM
// Home Page: http://members.fortunecity.com/neshkov/dj.html  http://www.neshkov.com/dj.html - Check often for new version!
// Decompiler options: packimports(3) 
// Source File Name:   MazeDisplayPanel.java

import java.awt.*;
import javax.swing.JPanel;

public class MazeDisplayPanel extends JPanel
    implements MazeGeneratorListener, MazeSolverListener
{

    public MazeDisplayPanel()
    {
        currentMaze = null;
        currentSolution = null;
        animateGeneration = false;
        animateSolution = false;
        setBackground(BACKGROUND_COLOR);
        setOpaque(true);
    }

    public void startingMazeGeneration(Maze maze)
    {
        if(animateGeneration)
        {
            setCurrentMaze(maze);
            try
            {
                Thread.sleep(40L);
            }
            catch(InterruptedException interruptedexception) { }
        }
    }

    public void mazeModified(Maze maze)
    {
        if(animateGeneration)
        {
            setCurrentMaze(maze);
            try
            {
                Thread.sleep(40L);
            }
            catch(InterruptedException interruptedexception) { }
        }
    }

    public void startingMazeSolution(MazeSolution mazesolution)
    {
        if(animateSolution)
        {
            setCurrentSolution(mazesolution);
            try
            {
                Thread.sleep(40L);
            }
            catch(InterruptedException interruptedexception) { }
        }
    }

    public void solutionChanged(MazeSolution mazesolution)
    {
        if(animateSolution)
        {
            setCurrentSolution(mazesolution);
            try
            {
                Thread.sleep(40L);
            }
            catch(InterruptedException interruptedexception) { }
        }
    }

    public Maze getCurrentMaze()
    {
        return currentMaze;
    }

    public void setCurrentMaze(Maze maze)
    {
        currentMaze = maze;
        repaint();
    }

    public MazeSolution getCurrentSolution()
    {
        return currentSolution;
    }

    public void setCurrentSolution(MazeSolution mazesolution)
    {
        currentSolution = mazesolution;
        repaint();
    }

    public void setAnimateGeneration(boolean flag)
    {
        animateGeneration = flag;
    }

    public void setAnimateSolution(boolean flag)
    {
        animateSolution = flag;
    }

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        if(currentMaze != null)
        {
            paintMaze(g);
            if(currentSolution != null)
                paintSolution(g);
        }
    }

    private void paintMaze(Graphics g)
    {
        g.setColor(WALL_COLOR);
        for(int i = 0; i < currentMaze.getWidth(); i++)
        {
            for(int j = 0; j < currentMaze.getHeight(); j++)
            {
                Rectangle rectangle = getRectangleFromPosition(g, i, j);
                if(!currentMaze.canMoveNorthFrom(i, j))
                {
                    boolean flag = true;
                    if((i == currentMaze.getStartXPosition() && j == currentMaze.getStartYPosition() || i == currentMaze.getEndXPosition() && j == currentMaze.getEndYPosition()) && i != 0 && j == 0)
                        flag = false;
                    if(flag)
                        g.drawLine(rectangle.x, rectangle.y, rectangle.x + rectangle.width, rectangle.y);
                }
                if(!currentMaze.canMoveWestFrom(i, j))
                {
                    boolean flag1 = true;
                    if((i == currentMaze.getStartXPosition() && j == currentMaze.getStartYPosition() || i == currentMaze.getEndXPosition() && j == currentMaze.getEndYPosition()) && i == 0)
                        flag1 = false;
                    if(flag1)
                        g.drawLine(rectangle.x, rectangle.y, rectangle.x, rectangle.y + rectangle.height);
                }
            }

        }

        for(int k = 0; k < currentMaze.getWidth(); k++)
        {
            Rectangle rectangle1 = getRectangleFromPosition(g, k, currentMaze.getHeight() - 1);
            if(!currentMaze.canMoveSouthFrom(k, currentMaze.getHeight() - 1))
            {
                boolean flag2 = true;
                if((k == currentMaze.getStartXPosition() && currentMaze.getStartYPosition() == currentMaze.getHeight() - 1 || k == currentMaze.getEndXPosition() && currentMaze.getEndYPosition() == currentMaze.getHeight() - 1) && k != 0 && k != currentMaze.getWidth() - 1)
                    flag2 = false;
                if(flag2)
                    g.drawLine(rectangle1.x, rectangle1.y + rectangle1.height, rectangle1.x + rectangle1.width, rectangle1.y + rectangle1.height);
            }
        }

        for(int l = 0; l < currentMaze.getHeight(); l++)
        {
            Rectangle rectangle2 = getRectangleFromPosition(g, currentMaze.getWidth() - 1, l);
            if(!currentMaze.canMoveEastFrom(currentMaze.getWidth() - 1, l))
            {
                boolean flag3 = true;
                if(currentMaze.getStartXPosition() == currentMaze.getWidth() - 1 && l == currentMaze.getStartYPosition() || currentMaze.getEndXPosition() == currentMaze.getWidth() - 1 && l == currentMaze.getEndYPosition())
                    flag3 = false;
                if(flag3)
                    g.drawLine(rectangle2.x + rectangle2.width, rectangle2.y, rectangle2.x + rectangle2.width, rectangle2.y + rectangle2.height);
            }
        }

    }

    private void paintSolution(Graphics g)
    {
        g.setColor(SOLUTION_COLOR);
        int i = currentSolution.getLength();
        int ai[] = currentSolution.getXPositions();
        int ai1[] = currentSolution.getYPositions();
        Point point = getCenterPointFromPosition(g, ai[0], ai1[0]);
        g.drawLine(point.x, point.y, point.x, point.y);
        for(int j = 1; j < i; j++)
        {
            Point point1 = getCenterPointFromPosition(g, ai[j - 1], ai1[j - 1]);
            Point point2 = getCenterPointFromPosition(g, ai[j], ai1[j]);
            g.drawLine(point1.x, point1.y, point2.x, point2.y);
        }

    }

    private Rectangle getRectangleFromPosition(Graphics g, int i, int j)
    {
        if(currentMaze == null)
        {
            return new Rectangle(0, 0, 0, 0);
        } else
        {
            Insets insets = getInsets();
            int k = getWidth() - insets.left - insets.right - 1;
            int l = getHeight() - insets.top - insets.bottom - 1;
            double d = ((double)k * (double)i) / (double)currentMaze.getWidth();
            double d1 = ((double)k * (double)(i + 1)) / (double)currentMaze.getWidth();
            double d2 = ((double)l * (double)j) / (double)currentMaze.getHeight();
            double d3 = ((double)l * (double)(j + 1)) / (double)currentMaze.getHeight();
            return new Rectangle(insets.left + (int)d, insets.top + (int)d2, (int)d1 - (int)d, (int)d3 - (int)d2);
        }
    }

    private Point getCenterPointFromPosition(Graphics g, int i, int j)
    {
        if(currentMaze == null)
        {
            return new Point(0, 0);
        } else
        {
            Rectangle rectangle = getRectangleFromPosition(g, i, j);
            return new Point(rectangle.x + rectangle.width / 2, rectangle.y + rectangle.height / 2);
        }
    }

    private Maze currentMaze;
    private MazeSolution currentSolution;
    private boolean animateGeneration;
    private boolean animateSolution;
    private static final Color BACKGROUND_COLOR;
    private static final Color WALL_COLOR;
    private static final Color SOLUTION_COLOR;

    static 
    {
        BACKGROUND_COLOR = Color.white;
        WALL_COLOR = Color.black;
        SOLUTION_COLOR = Color.red;
    }
}
